﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


namespace WebLicenseGenerator.UC
{
	public partial class MaskedDate : System.Web.UI.UserControl
	{
		bool _validateEmptyDate = true;

		public bool ValidateEmptyDate
		{
			get { return _validateEmptyDate; }
			set { _validateEmptyDate = value; }
		}
		public string Text
		{
			get
			{
				if (mskDateTxt.Text == "__/__/____")
					return String.Empty;
				else
					return mskDateTxt.Text;
			}
			set { mskDateTxt.Text = value; }
		}
		public bool IsEmpty
		{
			get { return (String.IsNullOrEmpty(mskDateTxt.Text)); }
		}
		public bool IsValid
		{
			get
			{
				cvInvalidDate.Validate();

				if (ValidateEmptyDate)
				{
					cvBlankDate.Validate();
					return cvBlankDate.IsValid && cvInvalidDate.IsValid;
				}
				else
					return cvInvalidDate.IsValid;
			}
		}

        public delegate void TextChangedHandler(object sender, EventArgs e);
        public event TextChangedHandler TextChanged;

		protected void Page_Load(object sender, EventArgs e)
		{
		}

        protected void Txt_TextChanged(object sender, EventArgs e)
        {
            if (TextChanged != null)
                TextChanged(sender, e);
        }

		protected void ValidateBlankDate(object sender, ServerValidateEventArgs args)
		{
			args.IsValid = !(this.ValidateEmptyDate && String.IsNullOrEmpty(mskDateTxt.Text));
		}

		protected void ValidateInvalidDate(object sender, ServerValidateEventArgs args)
		{
			DateTime dt;

			try
			{
				if (!this.IsEmpty)
					dt = Convert.ToDateTime(mskDateTxt.Text);

				args.IsValid = true;
			}
			catch
			{
				args.IsValid = false;
			}
		}
	}
}